<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title></title>
</head>
<body>

<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
            </div>
            <input type="text" name="parkName" value="" placeholder="停车场名称" class="layui-input" lay-affix="clear">
        </div>
    </div>

    <div class="layui-btn-container layui-col-xs12">
        <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>
<button class="layui-btn" lay-on="e1">添加</button>
<table class="layui-hide" id="ID-table-demo-search"></table>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>

    </div>
</script>
<!--添加停车场的弹出-->
<div style="display: none"  id="addForm">
    <form class="layui-form" action="" lay-filter="addForm" >
        <input type="hidden" name="id"  placeholder="请输入" autocomplete="off"
               class="layui-input">

        <div class="layui-form-item">
            <label class="layui-form-label">停车场名称</label>
            <div class="layui-input-block">
                <input type="text" name="parkName" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">车位数量</label>
            <div class="layui-input-block">
                <input type="text" name="parkLot" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">免费时长</label>
            <div class="layui-input-block">
                <input type="text" name="freeMinute" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">计时单位</label>
            <div class="layui-input-block">
                <input type="text" name="unit" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单元费用</label>
            <div class="layui-input-block">
                <input type="text" name="price" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>
<script>
    layui.use(function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var util = layui.util;
        //  2.9+ 版本可省略 attr 参数，默认读取 lay-on
        util.on({
            e1: function () {
                // 在此处输入 layer 的任意代码
                layer.open({
                   type:1,
                    area: ['60%', '60%'],
                    title: '添加停车场',

                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content: $("#addForm"), // 这里content是一个普通的String

                });
            }
        });
        // 创建表格实例
        table.render({
            elem: '#ID-table-demo-search',
            url: '/park/list.do', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData: function (res) {
                return {
                    code: res.code,
                    data: res.data.list,
                    count: res.data.total,
                }
            },
            limit: 3,
            cols: [[
                {type: 'radio', title: '😊', fixed: true}, // 单选框
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: true},
                {field: 'parkName', title: '停车场名称', width: 180},
                {field: 'parkLot', title: '车位数量', width: 180, sort: true},
                {field: 'freeMinute', title: '免费时长(分钟)', width: 180},
                {field: 'price', title: '单元费用(元)', width: 180},
                {field: 'unit', title: '计时单元(分钟)', width: 180},
                {fixed: 'right', title:'操作', width: 134, minWidth: 125, templet: '#toolDemo'}
            ]],
            page: true,
            height: 310
        });
        // 日期
        laydate.render({
            elem: '.demo-table-search-date'
        });

        // 搜索提交
        form.on('submit(demo1)', function (data) {
            var field = data.field; // 获得表单字段
           $.post("/park/addOrUpdate.do",field,function (res) {
               if(res.code==0){
                   layer.msg(res.msg,{time:2000},function (){
                       layer.closeAll();//关闭弹出层
                       table.reload('ID-table-demo-search', {
                           page: {
                               curr: 1 // 重新从第 1 页开始
                           },

                       });
                   })
               }else{
                   layer.msg(res.msg,{time:2000});
               }
           })
            return false; // 阻止默认 form 跳转
        });


        // 搜索提交
        form.on('submit(demo-table-search)', function (data) {
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('ID-table-demo-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });
            layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
            return false; // 阻止默认 form 跳转
        });

        table.on('tool(ID-table-demo-search)', function(obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
             console.log(data)
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑 - id:' + data.id,
                    type: 1,
                    area: ['60%', '60%'],
                    content: $("#addForm"),
                    success:function (){
                        //为表单赋值
                        form.val("addForm", data);
                        //渲染表单
                        form.render();
                    }
                });
            }
        })
        });
</script>

</body>
</html>